// pages/share/share.js
import { Share } from '../../model/share.js'
const share = new Share();
var WxParse = require("../../wxParse/wxParse.js")
Page({

    /**
     * 页面的初始数据
     */
    data: {
        indicatorDots:false,
        id:'',
        imgUrls:[],
        shareText:{},
        worksId:'',
        workslist:[],
        count:0,
        page:0,
        picWidth:'',
        picHeight:'',
        isLiked:0,
        isJoin:'',
        loging:'没有更多了',
        isCanClick:true
    },
    
    handlerGobackClick() {
        // wx.switchTab({
        //     url:'/pages/firstpage/firstpage'
        // })
        wx.navigateBack({
            delta:1
        })
    },
    uploding:function(e){
        console.log(this.data.shareText.id)
        wx.navigateTo({
            url: '/pages/uploading/uploading?id='+this.data.shareText.id+'&isJoin='+this.data.isJoin,
          })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(options.id)
        this.setData({
            id:options.id
        })
        this.share()
        // this.shareList()
        // this.like_this_work()
    },
    share(){
        var obj={id:this.data.id}
        share.works_detail(obj).then(res=>{
            console.log(res)
            this.setData({
                imgUrls:res.data.picList,
                shareText:res.data.datum,
                isJoin:res.data.isJoin
            })
            var article=res.data.datum.content;
            WxParse.wxParse('article', 'html', article,this,1)
            console.log(this.data.array)
        }).catch(error=>{
            console.log(error)
        })
    },
    shareList(){
        // this.setData({
        //     workslist:[]
        // })
        var obj={page:this.data.page,worksId:this.data.id}
        share.works_list(obj).then(res=>{
            var workslist = this.data.workslist
            for(var i=0;i<res.data.datums.length;i++){
                workslist.push(res.data.datums[i])
            }
            console.log(res)
            // var isLiked=this.data.isLiked
            this.setData({
                workslist:workslist,
                picHeight:res.data.datums.picHeight,
                picWidth:res.data.datums.picWidth,
                isLiked:res.data.datums.isLiked,
                count:res.data.count,
            })
            // console.log(this.data.type)
            if(res.data.count == workslist.length){
                this.setData({
                loging:'没有更多了'
            })
            }
            this.fillData(true, workslist);
        }).catch(error=>{
            console.log(error)
        })
    },
    //喜欢-取消喜欢接口
    // like_this_work(){
    //     var type=this.data.isLiked
    //     var obj={worksId:this.data.id,type:this.data.type}
    //     share.like_this_work(obj).then(res=>{
    //         console.log(res)
    //         console.log()
    //     }).catch(error=>{
    //         console.log(error)
    //     })
    // },

    getAddGrug(e) {
        // var items = this.data.categoryData[this.data.categoryCur].listData;
        console.log(e)
        
        if(this.data.isCanClick){
            console.log("进入方法了");
            this.setData({
                isCanClick:false
            })
            var worksId = e.detail.worksId;
            var type = e.detail.type;
            var params = {
             worksId: worksId,
            type: type
            }
            
            share.like_this_work(params).then(res=>{
                console.log(res)
                // console.log()
                
                var list = this.data.workslist
                
                if(res.code == 0){
                    console.log('11111111111111')
                    console.log(this.data.workslist)
                    
                    for(var i in list){
                        console.log('22222222222222')
                        if(list[i].id == params.worksId){
                            list[i].isLiked =res.data.isLiked
                            if(res.data.isLiked == 0){
                                console.log('33333333333333')
                                list[i].likeNum=Number(list[i].likeNum)-1
                            }else{
                                console.log('44444444444444')
                                list[i].likeNum=Number(list[i].likeNum)+1
                            }
                        }
                    }
                }else if(res.code==1){
                    wx.showToast({
                    title: res.msg,
                    icon:'none',
                    duration:3000
                    })
                }
                this.setData({
                    workslist:list
                })
                this.fillData(true, list);
                setTimeout(()=>{  //300毫秒延迟
                    this.setData({
                        isCanClick:true
                    })
                },1000)
                
            }).catch(error=>{
                console.log(error)
                setTimeout(()=>{  //300毫秒延迟
                    this.setData({
                        isCanClick:true
                    })
                },1000)
            })
        }
        
        
         
    },








    fillData: function (isFull, items) {
        let view = this.selectComponent('#waterFallView');
        view.fillData(isFull, items,'1');
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.setData({
            workslist:[],
            page:0
        })
        this.share()
        this.shareList()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if(this.data.count>this.data.workslist.length){
            this.setData({
                page:this.data.page+1,
                loging:'加载中...'
            })
            this.shareList()
        }else{
            this.setData({
                loging:'没有更多了'
            })
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})